IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'Proc_GetLineID')
BEGIN
	PRINT 'Dropping Procedure Proc_GetLineID'
	DROP  Procedure  Proc_GetLineID
END
GO
PRINT 'Creating Procedure Proc_GetLineID'
GO
CREATE Procedure Proc_GetLineID
	 @aTable_nam varchar(20)
AS
declare @lLine_id int;
declare @lLine_idCount int;
declare @lNewdate datetime;
declare @aNewLine_id varchar(10)
select @lNewDate=(select  Convert(datetime ,convert(char(10),getdate(),101 )))
select  @lLine_idCount =(select count(Line_id) from NoRecordor 
  where Table_Nam=@aTable_nam)

select  @lLine_id=( select Line_id from NoRecordor 
  where Table_Nam=@aTable_nam )
if @lLine_idCount=1
begin
  Update NoRecordor set Line_id=Line_id+1 ,LastIdDate=@lNewDate
   where Table_Nam=@aTable_nam
  
   select @lLine_id = @lLine_id + 1
end 
else
if @lLine_idCount=0
begin
  insert into NoRecordor(Table_nam,LastBillDate,Bill_no,LastIdDate,Line_id) 
    values(@aTable_nam, @lNewDate, 1,@lNewDate , 1)
  select @lLine_id = 1
end  
if (@lLine_id>0) and (@lLine_id<10)  
    select @aNewLine_id='00000'+ convert(char(1),@lLine_id)
  else 
  if (@lLine_id>9) and (@lLine_id<100)  
    select @aNewLine_id='0000'+ convert(char(2),@lLine_id)
  else 
  if (@lLine_id>99) and (@lLine_id<1000)  
    select @aNewLine_id='000'+ convert(char(3),@lLine_id)
  else 
  if (@lLine_id>999) and (@lLine_id<10000)  
    select @aNewLine_id= '00'+convert(char(4),@lLine_id)
  if (@lLine_id > 9999) and (@lLine_id < 100000)  
    select @aNewLine_id = '0' + convert(char(5), @lLine_id)
  if (@lLine_id > 99999) and (@lLine_id < 1000000)  
    select @aNewLine_id = convert(char(6), @lLine_id)
select @aNewLine_id

RETURN
GO
GRANT EXEC ON Proc_GetLineID TO PUBLIC
GO
